42_Automating Encoder Signal Detection

Because the MCU will not be able to constantly check every encoder’s input constantly while performing other calculations, a sort of interrupt would need to be used to monitor the encoder pulses.  Whenever the pulse signal changes, an interrupt can be set off, and the interrupt handler function can modify an integer keeping track of how many pulses have been received.  If the motor is traveling clockwise, each pulse could be added to the integer; if the motor is traveling counter-clockwise, each pulse could be subtracted from the integer or vice versa.  This integer can be converted into the motor’s rotational position, knowing how many pulses it takes to rotate the output shaft.

To automatically detect when the encoder pulse signals changed, the MCU’s change notification system was used.  Only certain pins can accommodate change notification interrupts, so the circuit schematic may need to be modified to ensure that the GPIO pins for the encoders have change notification (CN).  Figure 1 shows the two functions used to setup the CN system.  The encoder_init( ) function enables the CN functionality on the input pin, enables the interrupt, and resets the interrupt flag.  The _CNInterrupt( ) function is called whenever the pin’s interrupt flag is set.  This function increases an integer variable to keep track of how many pulses are received, changes the status of the indicator LED, and resets the interrupt flag.
Figure 1:  Initialization and Interrupt Handler Functions For Change Notification Pin

The MCU’s CN functionality allowed the program’s main loop to remain empty while encoder pulses were handled in the background.  The problem of the MCU resetting every 10 seconds or so was still happening.  Reading about possible causes for this, I tried disabling the Watchdog Timer in the programs configuration bits.  This seemed to work as the program did not restart on its own anymore after that.  The Watchdog Timer (WTD) is supposed to reset the device if a processing malfunction is detected; so in the previous test after the count integer had reached the max value and had the motor duty cycle set to 0%, the WTD was probably detecting that the main loop was repeating without accomplishing anything (once the max. integer value was reached, all the main loop was doing was checking to see if the integer was less than that value).